package com.devfest.wishmaster.gui;

import java.text.SimpleDateFormat;
import java.util.List;

import com.devfest.wishmaster.R;
import com.devfest.wishmaster.model.WishItem;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailAdapter extends BaseAdapter {

	List<WishItem> wishItems;
	Context context;
	LayoutInflater mInflater;

	public DetailAdapter(Context context, List<WishItem> wishItems) {
		this.wishItems = wishItems;
		this.context = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return wishItems.size();
	}

	@Override
	public WishItem getItem(int position) {
		return wishItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return wishItems.get(position).getId();
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater
					.inflate(R.layout.wish_item_list_inner, null);
			holder.title = (TextView) convertView
					.findViewById(R.id.inner_item_title);
			holder.description = (TextView) convertView
					.findViewById(R.id.inner_description);
			holder.dateCreated = (TextView) convertView
					.findViewById(R.id.inner_date_created);
			holder.image = (ImageView) convertView
					.findViewById(R.id.inner_imageView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(wishItems.get(position).getTitle());
		holder.description.setText(wishItems.get(position).getDescription());
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		holder.dateCreated.setText(formatter.format(wishItems.get(position)
				.getDateCreated()));
		if (wishItems.get(position).getImage() != null) {
		    Bitmap bmp = wishItems.get(position).getImage();
		    int width=150;
		    int height=150;
		    bmp=Bitmap.createScaledBitmap(bmp, width,height, true);
		    
			holder.image.setImageBitmap(bmp);
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView title;
		TextView description;
		TextView dateCreated;
		ImageView image;
	}
}